package schooltest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.junit.Test;

import school.Person;

public class PersonTest {

	Person person = new Person();
	SimpleDateFormat dateFormatter = new SimpleDateFormat("MM/dd/yyyy");

	@Test
	public void testGetFirstName() {
		String firstName = "FirstName";
		person.setFirstName(firstName);
		assertEquals(firstName, person.getFirstName());
		firstName = "";
		person.setFirstName(firstName);
		assertEquals(firstName, person.getFirstName());
	}

	@Test
	public void testSetFirstName() {
		String firstName = "FirstName";
		person.setFirstName(firstName);
		assertEquals(firstName, person.getFirstName());
		firstName = "";
		person.setFirstName(firstName);
		assertEquals(firstName, person.getFirstName());
	}

	@Test
	public void testGetLastName() {
		String lastName = "lastName";
		person.setLastName(lastName);
		assertEquals(lastName, person.getLastName());
		lastName = "";
		person.setLastName(lastName);
		assertEquals(lastName, person.getLastName());
	}

	@Test
	public void testSetLastName() {
		String lastName = "lastName";
		person.setLastName(lastName);
		assertEquals(lastName, person.getLastName());
		lastName = "";
		person.setLastName(lastName);
		assertEquals(lastName, person.getLastName());
	}

	@Test
	public void testGetDateOfBirth() {
		Date DOB = null;
		person.setDateOfBirth(DOB);
		assertEquals(DOB, person.getDateOfBirth());

		try {
			DOB = dateFormatter.parse("11/1/1991");

		} catch (Exception e) {
			fail(e.toString());
		}

		person.setDateOfBirth(DOB);
		assertEquals(DOB, person.getDateOfBirth());

	}

	@Test
	public void testSetDateOfBirth() {
		testGetDateOfBirth();
	}

	@Test
	public void testGetAge() {
		Calendar clndr = Calendar.getInstance();
		clndr.setTimeInMillis(System.currentTimeMillis());
		clndr.set(Calendar.YEAR, clndr.get(Calendar.YEAR) - 30); // 30 years old
		Date DOB = clndr.getTime();
		person.setDateOfBirth(DOB);
		assertEquals("30 years", person.getAge());

		DOB = clndr.getTime();
		person.setDateOfBirth(DOB);
		assertEquals("30 years and 1 months", person.getAge());

		clndr.setTimeInMillis(System.currentTimeMillis());
		rollMonth(clndr, -3);
		DOB = clndr.getTime();
		person.setDateOfBirth(DOB);
		assertEquals("3 months", person.getAge());

		clndr.setTimeInMillis(System.currentTimeMillis());
		rollMonth(clndr, -35);
		DOB = clndr.getTime();
		person.setDateOfBirth(DOB);
		assertEquals("35 months", person.getAge());

		clndr.setTimeInMillis(System.currentTimeMillis());
		rollMonth(clndr, -36);
		DOB = clndr.getTime();
		person.setDateOfBirth(DOB);
		assertEquals("3 years", person.getAge());

		clndr.setTimeInMillis(System.currentTimeMillis());
		rollMonth(clndr, -38);
		DOB = clndr.getTime();
		person.setDateOfBirth(DOB);
		assertEquals("3 years and 2 months", person.getAge());

		clndr.setTimeInMillis(System.currentTimeMillis());
		rollMonth(clndr, 3);
		DOB = clndr.getTime();
		person.setDateOfBirth(DOB);
		assertEquals("Unknown", person.getAge());
	}

	private int rollMonth(Calendar clndr, int val) {
		int month = clndr.get(Calendar.MONTH);
		month += val;
		while (month < 0) {
			month = 12 + month;
			clndr.set(Calendar.YEAR, clndr.get(Calendar.YEAR) - 1);
		}
		clndr.set(Calendar.MONTH, month);
		return month;
	}

}
